﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace IM.Client
{
    /// <summary>
    /// 即时通信主界面
    /// </summary>
    public partial class MainIMWindow : Window
    {
        #region IM登陆切换界面管理
        /// <summary>
        /// 登陆窗口
        /// </summary>
        private LoginWindow loginWindow;
        /// <summary>
        /// IM装载模块显示界面
        /// </summary>
        private IMLodingInfo imLoadinfo;
        /// <summary>
        /// 登陆后显示的主窗体。
        /// </summary>
        private IMMian mainIMWindow;
        #endregion

        private AutoResetEvent flagEvent = new AutoResetEvent(true);

        public MainIMWindow()
        {
            InitializeComponent();
            InitLoginForm();
        }

        private void InitLoginForm()
        {
            //初始化页面
            InitializeCurrentActivedWindow();
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        private void InitializeCurrentActivedWindow()
        {
            #region 加载页面

            //清空控件内的所有子控件
            this.ContentContainer.Children.Clear();

            imLoadinfo = new IMLodingInfo();

            //添加load控件
            this.ContentContainer.Children.Add(imLoadinfo);

            //this.Show();

            #endregion

            System.Threading.Thread tempThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(InitializeIM));
            tempThread.IsBackground = true;
            tempThread.Start();

            //this.Dispatcher.BeginInvoke(new Action(InitializeIM));
        }

        private void InitializeIM(object para)
        {
            System.Threading.Thread.Sleep(4000);

            #region 登陆主界面

            this.Dispatcher.Invoke(new Action(() =>
            {
                //清空控件内的所有子控件
                this.ContentContainer.Children.Clear();

                mainIMWindow = new IMMian();

                this.ContentContainer.Children.Add(mainIMWindow);
            }));

            #endregion
        }

        /// <summary>
        /// 退出IM登陆。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            //通过广播发送到服务器，服务器收到后，给所有的IM客户端广播消息。
            this.Close();
        }

        /// <summary>
        /// 鼠标左键按下的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //加载完毕后，执行相关的操作，
           // System.Threading.Thread.Sleep(3000);

           // InitializeIM();
        }
    }
}
